package com.cskaoyan.order.biz.handler;

import com.cskaoyan.order.biz.context.CreateOrderContext;
import com.cskaoyan.order.biz.context.TransHandlerContext;
import com.cskaoyan.order.dto.CartProductDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Component
public class ValidateHandler extends AbstractTransHandler {


    @Override
    public boolean isAsync() {
        return false;
    }

    /**
     * 1.验证订单
     *
     * @param context
     * @return boolean
     * @author lwq
     * @since 2022/12/03 10:54
     */
    @Override
    public boolean handle(TransHandlerContext context) {
        CreateOrderContext createOrderContext = (CreateOrderContext) context;
        List<CartProductDto> cartProductDtoList = createOrderContext.getCartProductDtoList();
        // 生成订单中的商品idList
        List<Long> buyProductIds = new ArrayList<>();
        for (CartProductDto cartProductDto : cartProductDtoList) {
            buyProductIds.add(cartProductDto.getProductId());
        }
        createOrderContext.setBuyProductIds(buyProductIds);
        createOrderContext.setBuyerNickName(createOrderContext.getUserName());
        return true;
    }
}
